/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.smp;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.interfaces.smp.CPUInterface;
import br.gov.cemaden.library.model.smp.CPU;
import br.gov.cemaden.library.model.smp.ExecHardware;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author paulo.dias
 */
public class CPUDao extends GenericDao implements CPUInterface {

    private Session session;

    public CPUDao(Session session) {
        this.session = session;
    }

    public List<CPU> findAll() {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<CPU> cpu = null;
        try {
            Query query = session.getNamedQuery("CPU.findAll");
            cpu = (List<CPU>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no CPUFacade.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return cpu;
    }

    public CPU findById(Long id) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        CPU cpu = null;
        try {
            Query query = session.getNamedQuery("CPU.findById").setParameter("id", id);
            cpu = (CPU) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no CPUFacade.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return cpu;
    }

    public CPU findByTotalSystemTime(String totalSystemTime) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        CPU cpu = null;
        try {
            Query query = session.getNamedQuery("CPU.findByTotalSystemTime").setParameter("totalSystemTime", totalSystemTime);
            cpu = (CPU) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no CPUFacade.findByTotalSystemTime: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return cpu;
    }

    public CPU findByCacheSize(Double cacheSize) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        CPU cpu = null;
        try {
            Query query = session.getNamedQuery("CPU.findByCacheSize").setParameter("cacheSize", cacheSize);
            cpu = (CPU) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no CPUFacade.findByCacheSize: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return cpu;
    }

    public CPU findByCorPerSockets(Integer corPerSockets) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        CPU cpu = null;
        try {
            Query query = session.getNamedQuery("CPU.findByCorPerSockets").setParameter("corPerSockets", corPerSockets);
            cpu = (CPU) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no CPUFacade.findByCorPerSockets: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return cpu;
    }

    public CPU findByModel(String model) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        CPU cpu = null;
        try {
            Query query = session.getNamedQuery("CPU.findByModel").setParameter("model", model);
            cpu = (CPU) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no CPUFacade.findByModel: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return cpu;
    }

    public CPU findByTotalCores(Integer totalCores) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        CPU cpu = null;
        try {
            Query query = session.getNamedQuery("CPU.findByTotalCores").setParameter("totalCores", totalCores);
            cpu = (CPU) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no CPUFacade.findByTotalCores: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return cpu;
    }

    public CPU findByTotalSockets(Integer totalSockets) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        CPU cpu = null;
        try {
            Query query = session.getNamedQuery("CPU.findByTotalSockets").setParameter("totalSockets", totalSockets);
            cpu = (CPU) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no CPUFacade.findByTotalSockets: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return cpu;
    }

    public CPU findByVendor(String vendor) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        CPU cpu = null;
        try {
            Query query = session.getNamedQuery("CPU.findByVendor").setParameter("vendor", vendor);
            cpu = (CPU) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no CPUFacade.findByVendor: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return cpu;
    }
    
    public CPU findByCpuUsage(Double cpuUsage) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        CPU cpu = null;
        try {
            Query query = session.getNamedQuery("CPU.findByCpuUsage").setParameter("cpuUsage", cpuUsage);
            cpu = (CPU) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no CPUFacade.findByCpuUsage: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return cpu;
    }

    public List<CPU> findByExecHardware(ExecHardware execHardware) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<CPU> cpu = null;
        try {
            Query query = session.getNamedQuery("CPU.findByExecHardware").setParameter("execHardware", execHardware);
            cpu = (List<CPU>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no CPUFacade.findByExecHardware: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return cpu;
    }

    public CPU insert(CPU cpu) {
        try {
            saveOrUpdate(this.session, cpu);
        } catch (Exception ex) {
            Logger.getLogger(CPUDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return cpu;
    }

    public CPU update(CPU cpu) {
        try {
            saveOrUpdate(this.session, cpu);
        } catch (Exception ex) {
            Logger.getLogger(CPUDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return cpu;
    }

    public void delete(CPU cpu) {
        try {
            remove(this.session, cpu);
        } catch (Exception ex) {
            Logger.getLogger(CPUDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
